package com.gshrms.common.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.gshrms.common.constant.DictConstants;

import java.util.Random;

public class CodeUtils {

    private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    private static final String NUMBERS = "0123456789";

    /**
     * 生成组织随机编号
     * @return
     */
    public static String buildOrgNo(){
        return buildRandomNumber(6);
    }

    /**
     * 生成人员编号
     * @param idcard 身份证号
     * @return
     */
    public static String buildStaffNo(String idcard){
        // 身份证号后8位
        return idcard.substring(idcard.length() - 8);
    }

    /**
     * 生成appId
     * @return
     */
    public static String buildApiAppId(){
        return IdUtil.simpleUUID();
    }

    /**
     * 生成appSecret
     * @param appId
     * @return
     */
    public static String buildApiAppSecret(String appId){
        return SecureUtil.sha1(appId + buildRandomString(8));
    }

    /**
     * 生成简单UUID
     * @return
     */
    public static String buildSimpleUUID(){
        return IdUtil.simpleUUID();
    }

    /**
     * 随机生成指定长度的字符串
     * @return
     */
    public static String buildRandomString(int length) {
        Random random = new Random();
        StringBuilder sb = new StringBuilder(length);

        for (int i = 0; i < length; i++) {
            int index = random.nextInt(CHARACTERS.length());
            sb.append(CHARACTERS.charAt(index));
        }

        return sb.toString();
    }

    /**
     * 随机生成指定长度的数字字符串
     * @return
     */
    public static String buildRandomNumber(int length) {
        Random random = new Random();
        StringBuilder sb = new StringBuilder(length);

        for (int i = 0; i < length; i++) {
            int index = random.nextInt(NUMBERS.length());
            sb.append(NUMBERS.charAt(index));
        }

        return sb.toString();
    }
}
